/* Copyright - Apache License 2.0
 * 
 * The project "kyou" is
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *      http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.kyou.pack.segment;

import java.nio.charset.Charset;

import net.kyou.pack.PackContext;
import net.kyou.pack.StyleItem;
import net.kyou.pack.segment.param.ParamType;
import net.kyou.util.KyouByteOutputStream;
import net.kyou.util.KyouString;

import org.w3c.dom.Element;

/**
 * 文本段
 * <p>
 * 用于将段中的文本原样地输出到报文流中<br/>
 * <li>示例：</li>
 * &lt;text&gt;helloworld&lt;/text&gt;
 * <li>输出： helloworld</li>
 * <li>参数：无任何参数</li>
 * </p>
 * 
 * @author NuclearG (<a href="mailto:nuclearg@163.com">nuclearg@163.com</a>)
 */
@SegmentTag(name = "text", type = ParamType.Irrelevant)
class TextSegment extends Segment {
    /**
     * 该段包含的文本
     */
    private final byte[] text;

    /**
     * 初始化一个文本段
     */
    TextSegment(Element e, Charset encoding, StyleItem style) {
        super(style);
        this.text = KyouString.parse(e.getTextContent().trim(), encoding);
    }

    /**
     * 初始化一个文本段
     */
    TextSegment(String text, Charset encoding, StyleItem style) {
        super(style);
        this.text = KyouString.parse(text, encoding);
    }

    @Override
    public void __export(PackContext context, KyouByteOutputStream s) {
        s.write(this.text);
    }
}
